﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;

namespace SupplyChainManagement
{
    //to access the database
    public class DBAccess
    {
        string strConnect = "DSN=MySQL;UID=root;PWD=root;DATABASE=cp2011";

        OdbcConnection dbMySQL;

        public void OpenDatabase()
        {
            dbMySQL = new OdbcConnection(strConnect);
        }

        public void CloseDatabase()
        {
            if (dbMySQL != null)
                dbMySQL.Close();
        }

        public void SelectQuery(string command)
        {
            try
            {
                //open database
                dbMySQL.Open();

                OdbcCommand sqlCommand = dbMySQL.CreateCommand();
                //assign the SQL command
                sqlCommand.CommandText = command;

                //reader executes the select command
                OdbcDataReader sqlReader = sqlCommand.ExecuteReader();
                
                //close readers and dispose the commands
                sqlReader.Close();
                sqlCommand.Dispose();
                
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
            }
        }
    }
}
